package configs

import org.apache.logging.log4j.LogManager
import server.Timer
import java.io.*
import java.util.*

object OpcodeConfig {

    private val log = LogManager.getLogger()
    private val blockops = ArrayList<String>()

    fun load() {
        val file = File("./config/opcode.properties")
        if (!file.exists()) {
            log.error("读取opcode配置文件失败")
            return
        }
        Timer.WorldTimer.getInstance().register({
            try {
                blockops.clear()
                val props = Properties()
                val fileInputStream = FileInputStream(file)
                val inputStreamReader = InputStreamReader(fileInputStream, "GBK")
                val buff = BufferedReader(inputStreamReader)
                props.load(buff)
                buff.close()
                inputStreamReader.close()
                fileInputStream.close()

                val sendops = props.getProperty("sendops")
                val recvops = props.getProperty("recvops")

                sendops.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray().mapTo(blockops) { "S_$it" }

                recvops.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray().mapTo(blockops) { "R_$it" }

            } catch (e: IOException) {
                e.printStackTrace()
            }
        }, 1000, 1000)
    }

    fun isblock(ops: String, issend: Boolean): Boolean {
        return blockops.contains(if (issend) "S_$ops" else "R_$ops")
    }
}
